package com.jiyun.controller;

import com.jiyun.pojo.News;
import com.jiyun.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    // 处理获取新闻列表的请求，映射到news.html页面
//    获取所有的新闻列表
    @GetMapping("/all")
    public List<News> getAllNews() {
        return newsService.getAll();
    }

    // 处理查看新闻详情的请求，根据新闻id获取详情并映射到news-detail.html页面
    @GetMapping("/detail/{id}")
    public News newsDetail(@PathVariable("id") Integer id) {
        News news = newsService.getNewsById(id);
         return news;
    }
}
